package com.xzz.directprint.service;

/**
 * @Author: zhanglm
 * @CreateDate: 2024-12-03 14:25
 * @FullNameClass: com.xzz.directprint.service.printCadFile
 * @Description:
 */
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PrinterCADService {

    public void printCadFile(String cadFilePath) {
        // 确保文件存在
        File file = new File(cadFilePath);
        if (!file.exists()) {
            throw new IllegalArgumentException("CAD文件不存在");
        }

        // 构建打印命令
        List<String> commands = new ArrayList<>();
        commands.add("cmd.exe");
        commands.add("/c");
        commands.add("cad.exe"); // 假设AutoCAD的可执行文件名为cad.exe
        commands.add(cadFilePath);
        commands.add("/p"); // 打印命令
        commands.add("-printer"); // 指定打印机
        commands.add("YourPrinterName"); // 替换为你的打印机名称

        try {
            // 执行打印命令
            ProcessBuilder builder = new ProcessBuilder(commands);
            Process process = builder.start();

            // 等待打印进程结束
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new RuntimeException("打印命令执行失败", e);
        }
    }
}
